[#import "/framework/JGP.htm"  as JGP]
<!--
项目   jgp-cloud-parent
作者   LOUFEI [Gavin]
时间   2019-11-03
-->
[@JGP.head "系统用户"/]
[@JGP.body]
    <jgp-div _col="['*','500']" _cunit="px" _aux="true">
        <jgp-div slot="1-1" _row="['30','110','*']" _runit="px" _aux="true">
            <div slot="1-1">
                <jgp-btn _fn="createSystemUser">创建</jgp-btn>
                <jgp-btn _color="caution" _fn="deleteInBatch">批量删除</jgp-btn>
            </div>
            <jgp-query slot="2-1" _id="query-form" _target="grid" _action="/security/user-api-ctl/query-system-user">
                <jgp-form-group _title="查询条件">
                    <jgp-drop _col="4" _name="institutionId" _placeholder="选择机构" _mode="tree" _tree_extend="true" _url="/security/institution-api-ctl/query-tree"></jgp-drop>
                    <jgp-text _col="4" _name="realName" _placeholder="请输入真实姓名"></jgp-text>
                    <jgp-text _col="4" _name="username" _placeholder="请输入用户名"></jgp-text>
                    <jgp-text _col="4" _name="tel" _placeholder="请输入手机号"></jgp-text>
                    <jgp-drop _col="4" _name="locked" _placeholder="账号状态" _list="[{'label':'','value':''},{'label':'锁定','value':true}]"></jgp-drop>
                </jgp-form-group>
            </jgp-query>
            <jgp-grid slot="3-1" _id="grid" _cols="['100','100','100','100','200','200']" _cunit="px"
                      _onclick="freshUserInstitutionAnRole"
                      _record_tools="[{'tip':'修改','fn':'editUser','icon':'edit'},
                      {'tip':'角色设置','fn':'editUserRole','icon':'users'},
                      {'tip':'调岗','fn':'userTransfer','icon':'mail-forward'},
                      {'tip':'调回','fn':'userTransferBack','icon':'mail-reply'},
                      {'tip':'机构设置','fn':'editUserInstitution','icon':'building-o'},
                      {'tip':'密码重置','fn':'resetPassword','icon':'key'},
                      {'tip':'锁定','fn':'lock','icon':'lock'},
                      {'tip':'解锁','fn':'unLock','icon':'unlock-alt'},
                      {'tip':'删除','fn':'deleteOne','icon':'trash-o'}]"></jgp-grid>
        </jgp-div>

        <jgp-div slot="1-2" _row="['*','*']" _runit="px">
            <jgp-panel slot="1-1" _title="角色信息" _default_tools="[]">
                <jgp-tree2 _id="roleTree" _url="/security/role-user-api-ctl/query-private-role-tree"
                           _enable="false"
                           _check="false"
                           _lazy="false"
                           _extend="true"
                           _show_root="false"
                           _root="角色" _single="true"></jgp-tree2>
            </jgp-panel>
            <jgp-panel slot="2-1" _title="机构信息" _default_tools="[]">
                <jgp-tree2 _id="institutionTree" _url="/security/institution-user-api-ctl/query-private-institution-tree"
                           _enable="false"
                           _check="false"
                           _lazy="false"
                           _extend="true"
                           _show_root="false"
                           _root="角色" _single="true"></jgp-tree2>
            </jgp-panel>
        </jgp-div>
    </jgp-div>
[/@JGP.body]

[@JGP.foot]
    <script>
        function createSystemUser() {
            var dialog = {
                'title': '创建系统管理员',
                'width': '320px',
                'height': '270px',
                'url': '/security/user-ctl/edit-system-user'
            };
            JGP.$dialog(dialog);
        }

        function resetPassword(args) {
            JGP.$confirm("是否确定重置密码？",function () {
                JGP.$post("/security/user-api-ctl/reset-password",{
                    userId:args.back.record.id
                },function (result) {
                    if(result.flag){
                        JGP.$alert("重置成功!新密码("+result.data.pwd+")");
                    }
                })
            });

        }

        function deleteInBatch() {
            JGP.$confirm("是否确定删除？",function () {
                var selected = JGP.grid('grid').getSelected();
                var ids = [];
                for(var i=0;i<selected.length;i++){
                    ids.push(selected[i].id);
                }
                JGP.$postJson("/security/user-api-ctl/remove-system-user",ids,function (result) {
                    if(result.flag){
                        JGP.$alert("删除成功");
                        JGP.query('query-form').search();
                    }
                })
            });
        }

        function deleteOne(args) {
            JGP.$confirm("是否确定删除？", function () {
                var ids = [args.back.record.id];

                JGP.$postJson("/security/user-api-ctl/remove-system-user", ids, function (result) {
                    if (result.flag) {
                        JGP.$alert("删除成功");
                        JGP.query('query-form').search();
                    }
                })
            });
        }

        function editUser(args){
            var id = args.back.record.id;
            var dialog = {
                'title': '修改用户信息',
                'width': '320px',
                'height': '270px',
                'url': '/security/user-ctl/edit-system-user?id='+id
            };
            JGP.$dialog(dialog);
        }

        function editUserRole(args) {
            var userId = args.back.record.id;
            var dialog = {
                'title': '角色设置',
                'width': '320px',
                'height': '570px',
                'url': '/security/role-user-ctl/edit-user-role?userId='+userId,
                'callback':function (args) {
                    if(args){
                        freshUserRole(args)
                    }
                }
            };
            JGP.$dialog(dialog);
        }

        function editUserInstitution(args) {
            var userId = args.back.record.id;
            var dialog = {
                'title': '机构设置',
                'width': '320px',
                'height': '570px',
                'url': '/security/institution-user-ctl/edit-user-institution?userId='+userId,
                'callback':function (args) {
                    if(args){
                        freshUserInstitution(args)
                    }
                }
            };
            JGP.$dialog(dialog);
        }

        function lock(args) {
            var username = args.back.record.username;
            JGP.$confirm('是否锁定账号('+username+')?',function () {
                var userId = args.back.record.id;
                JGP.$post('/security/user-api-ctl/lock',{userId:userId},function () {
                    JGP.$alert('('+username+')已经锁定');
                })
            })
        }

        function unLock(args) {
            var username = args.back.record.username;
            JGP.$confirm('是否解锁账号('+username+')?',function () {
                var userId = args.back.record.id;
                JGP.$post('/security/user-api-ctl/un-lock',{userId:userId},function () {
                    JGP.$alert('('+username+')已经解锁');
                })
            })
        }

        function freshUserInstitutionAnRole(args) {
            var userId = args.back.record.id;
            freshUserInstitution(userId);
            freshUserRole(userId);
        }

        function freshUserInstitution(userId) {
            var tree = JGP.tree2("institutionTree");
            tree.attr('_ajax_param',{
                userId:userId
            })
            tree.reload();
        }

        function freshUserRole(userId) {
            var tree = JGP.tree2("roleTree");
            tree.attr('_ajax_param',{
                userId:userId
            })
            tree.reload();
        }
        function closeAndSearch() {
            JGP.$close();
            JGP.query('query-form').search();
        }

        function userTransfer(args){
            var userId = args.back.record.id;
            var dialog = {
                'title': '人员调转',
                'width': '320px',
                'height': '570px',
                'url': '/security/user-ctl/user-transfer?userId='+userId,
                'callback':function (args) {
                    if(args){
                        JGP.query('query-form').search();
                    }
                }
            };
            JGP.$dialog(dialog);
        }

        function userTransferBack(args){
            var username = args.back.record.username;
            JGP.$confirm('是否确定调回('+username+')?',function () {
                var userId = args.back.record.id;
                JGP.$post('/security/user-api-ctl/user-transfer-back',{userId:userId},function (result) {
                    if(result.flag){
                        JGP.query('query-form').search();
                    }else{
                        JGP.$warn(result.errors[0].msg)
                    }
                })
            })
        }


    </script>
[/@JGP.foot]